package net.comfycastle.musicplayer;

import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 메인 액티비티
 * @author ricale
 *
 */
public class MainActivity extends BaseActivity implements MainTabFragment.OnButtonClickListener, BaseListFragment.OnListItemClickListener {
	///////////
	//////////
	/////////   variable
	
	/** 모든 곡의 리스트 프래그먼트 */
	private SongListFragment  mFAllSong;
	/** 모든 앨범의 리스트 프래그먼트 */
	private AlbumListFragment mFAllAlbum;
	/** 모든 아티스트의 리스트 프래그먼트 */
	private GroupListFragment mFAllArtist;
	/** 모든 장르의 리스트 프래그먼트 */
	private GroupListFragment mFAllGenre;
	/** 모든 작곡가의 리스트 프래그먼트 */
	private GroupListFragment mFAllComposer;
	
	///////////
	//////////
	/////////   lifecycle callback method
	
	/**
	 * 액티비티가 생성되었을 때의 콜백 메서드
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		MusicUtility.bindMusicService(this);
		
		mFAllSong     = SongListFragment.newInstance(BaseListFragment.OBJECTID.ALLSONG);
		mFAllAlbum    = AlbumListFragment.newInstance(BaseListFragment.OBJECTID.ALLALBUM);
		mFAllArtist   = GroupListFragment.newInstance(BaseListFragment.OBJECTID.ALLARTIST,   MPApp.TAG_ARTIST);
		mFAllGenre    = GroupListFragment.newInstance(BaseListFragment.OBJECTID.ALLGENRE,    MPApp.TAG_GENRE);
		mFAllComposer = GroupListFragment.newInstance(BaseListFragment.OBJECTID.ALLCOMPOSER, MPApp.TAG_COMPOSER);
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		
		ft.add(R.id.main_container_basefragment, mFAllSong).hide(mFAllSong)
		  .add(R.id.main_container_basefragment, mFAllAlbum).hide(mFAllAlbum)
		  .add(R.id.main_container_basefragment, mFAllArtist).hide(mFAllArtist)
		  .add(R.id.main_container_basefragment, mFAllGenre).hide(mFAllGenre)
		  .add(R.id.main_container_basefragment, mFAllComposer).hide(mFAllComposer);
		
		ft.commit();
	} // end onCreate
	
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	} // end onConfigurationChanged
	
	/**
	 * 액티비티가 파괴될 때의 콜백 메서드
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		MusicUtility.unbindMusicService(this);
		if(!MusicUtility.isPlaying()) {
			Intent service = new Intent(this, MusicService.class);
			stopService(service);
		} else {			
			MusicUtility.finishService();
		} // end if
	} // end onDestroy
	
	///////////
	//////////
	/////////   event listener & callback method
	
	public void onButtonClick(MainTabFragment.BUTTONID buttonId) {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Log.i(this.getClass().getClass().getName(), "onButtonClick");
		
		ft.hide(mFAllSong)
		  .hide(mFAllAlbum)
		  .hide(mFAllArtist)
          .hide(mFAllGenre)
          .hide(mFAllComposer);
		
		switch(buttonId) {
		case ALLSONG:
			ft.show(mFAllSong);
			break;
		case ALLALBUM:
			ft.show(mFAllAlbum);
			break;
		case ALLARTIST:
			ft.show(mFAllArtist);
			break;
		case ALLGENRE:
			ft.show(mFAllGenre);
			break;
		case ALLCOMPOSER:
			ft.show(mFAllComposer);
			break;
		} // end switch
		
		ft.commit();
	} // end onButtonClick
	
	/**
	 * 프래그먼트의 아이템(항목)이 클릭되었을 때의 콜백 메서드
	 */
	@Override
	public void onItemClick(BaseListFragment.OBJECTID fragmentId, ListView l, View view, int position, long id) {
		Intent intent;
		MusicCursorAdapter adapter;
		Cursor cursor;
		TextView tvMain;
		
		switch(fragmentId) {
		case ALLALBUM:
			intent = new Intent(this, DetailActivity.class);
			intent.setAction(MPApp.ACT_SHOW_LIST_INALBUM);
			
			tvMain = (TextView)view.findViewById(R.id.listitem_albumlist_album);
			TextView tvArtist = (TextView)view.findViewById(R.id.listitem_albumlist_artist);
			
			adapter = (MusicCursorAdapter)mFAllAlbum.getListAdapter();
			cursor  = adapter.getCursor();
			String string = cursor.getString(cursor.getColumnIndex(MPApp.TAG_ALBUMARTIST));
			
			intent.putExtra(MPApp.ITT_GROUPNAME,     tvMain.getText());
			intent.putExtra(MPApp.ITT_TITLEARTIST,   tvArtist.getText());
			intent.putExtra(MPApp.ITT_ISALBUMARTIST, (string!=null) && !string.equals(""));
			break;
		case ALLARTIST:
			intent = new Intent(this, DetailActivity.class);
			intent.setAction(MPApp.ACT_SHOW_LIST_ALBUMS);
			
			adapter = (MusicCursorAdapter)mFAllArtist.getListAdapter();
			cursor  = adapter.getCursor();
			if(cursor.getString(cursor.getColumnIndex(MPApp.TAG_ALBUMARTIST)) == null) {
				intent.putExtra(MPApp.ITT_GROUPKIND, MPApp.TAG_ARTIST);
			} else {
				intent.putExtra(MPApp.ITT_GROUPKIND, MPApp.TAG_ALBUMARTIST);
			} // end if
			
			tvMain = (TextView)view.findViewById(R.id.listitem_grouplist_main);
			intent.putExtra(MPApp.ITT_GROUPNAME, tvMain.getText());
			break;
		case ALLGENRE:
			intent = new Intent(this, DetailActivity.class);
			intent.setAction(MPApp.ACT_SHOW_LIST_INGROUP);
			intent.putExtra(MPApp.ITT_GROUPKIND, mFAllGenre.getKind());
			
			tvMain = (TextView)view.findViewById(R.id.listitem_grouplist_main);
			intent.putExtra(MPApp.ITT_GROUPNAME, tvMain.getText());
			break;
		case ALLCOMPOSER:
			intent = new Intent(this, DetailActivity.class);
			intent.setAction(MPApp.ACT_SHOW_LIST_INGROUP);
			intent.putExtra(MPApp.ITT_GROUPKIND, mFAllComposer.getKind());
			
			tvMain = (TextView)view.findViewById(R.id.listitem_grouplist_main);
			intent.putExtra(MPApp.ITT_GROUPNAME, tvMain.getText());
			break;
		case ALLSONG: default:
			intent = new Intent(this, PlayActivity.class);
			intent.setAction(MPApp.ACT_PLAY);
			
			intent.putExtra(MPApp.ITT_GROUPKIND, mFAllSong.getKind());
			intent.putExtra(MPApp.ITT_GROUPNAME, mFAllSong.getName());
			intent.putExtra(MPApp.ITT_STARTTRACK, position);
			break;
		} // end switch
		
		startActivity(intent);
	} // end onListFragmentItemClick
} // end MainActivity